package sort;

import logger.*;
import java.io.*;
import java.util.Arrays;

/**
 *
 * @author BDSHADOW
 */
public class HugeArraySort implements CoolSorts {
    @Override
    public void sort(String input, String output)   {
        Logger logger = Logger.getInstance();
        try {
            logger.addFileAppender("logger.txt", true, 2000);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        logger.setPattern("%t - %l - %m");
        logger.setLevel(LogLevel.INFO);

        logger.trace("quaquauqua");
        logger.fatal("uzhs prosto");
        logger.error("bugbugbug");
        logger.stop();


        BigReadWrite data = new BigReadWrite();

        int[] a = data.readArray(input);
        //mergesort(a, 0, a.length-1);
        Arrays.sort(a);
        data.writeArray(a, output);
    }
    
        
     private static void mergesort (int[] a, int low, int high) { 
         if (low < high) {
             int m = (low+high)/2; 
             mergesort (a, low, m); 
             mergesort (a, m+1, high); 
             merge (a, low, m, high);
         }
     } 

     private static void merge (int[] a, int low, int m, int high) { 
         if (m+1>high) return; 
         int[] b = new int[a.length]; 
         for (int i=low; i!=m+1; i++) { 
              b[i] = a[i]; 
         } 
         
         for (int i=m+1; i!=high+1; i++) { 
              b[i] = a[high+m+1-i]; 
         }

         int k=low; int j=high;
         for (int i=low; i!=high+1; i++) { 
              if (b[k] <= b[j]) {
                  a[i] = b[k++];
              } else {
                   a[i] = b[j--];
              }
          } 
     }
}
